<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/8/2
 * Time: 13:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Notice extends Base
{
    /**
     * 消息首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $start_time = input('start_time');
            $end_time = input('end_time');
            $notice_title = input('notice_title');
            
            $where['notice_type'] = 1;
            if(isset($notice_title) && $notice_title != ''){
                $where['notice_title'] = ['like', '%' . $notice_title . '%'];
            }
            // 时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['notice_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['notice_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['notice_time'] = ['<', $end_time];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')->where($where)->count();// 获取总条数
            $lists = Db::name('notice')
                ->field('notice_id, notice_title, notice_range, notice_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('notice_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 发布系统消息
     * @return mixed|\think\response\Json
     */
    public function noticeAdd(){
        if(Request::instance()->isPost()){
            $notice_title = input('notice_title');
            $notice_content = input('notice_content');
            $notice_range = input('notice_range');
            $notice_user = input('notice_user');
            $notice = [
                'notice_title' => $notice_title,
                'notice_content' => $notice_content,
                'notice_range' => $notice_range,
                'notice_type' => 1,
                'notice_read_user' => ',0,',
                'notice_del_user' => ',0,',
                'notice_time' => date('Y-m-d H:i:s'),
            ];
            if($notice_range == 2){
                if(empty($notice_user)) return json(['code' => 100, 'msg' => '请选择需要发送的用户']);
                $notice['notice_user'] = ',' . $notice_user . ',';
            }else{
                $notice['notice_user'] = -1;
            }
            $res = Db::table('up_notice')->insert($notice);
            if($res){
                return json(['code' => 200, 'msg' => '发布成功']);
            }else{
                return json(['code' => 100, 'msg' => '发布失败']);
            }
        }
        $user = Db::table('up_u_user')->where('user_status', 1)->field('user_id, user_username')->select();
        $this->assign('user', $user);
        return $this->fetch('notice/noticeadd');
    }
    
    /**
     * 消息详情
     * @return mixed
     */
    public function noticeDetail(){
        $notice_id = input('notice_id');
        $notice = Db::table('up_notice')
            ->where('notice_id', $notice_id)
            ->field('notice_title, notice_content, notice_range, notice_user, notice_time')
            ->find();
        if($notice['notice_range'] == 2){
            $user_ids = trim($notice['notice_user'], ',');
            $notice_user = Db::table('up_u_user')
                ->where('user_id', 'IN', $user_ids)
                ->column('user_username');
            $notice['notice_user'] = implode(',', $notice_user);
        }
        $this->assign('notice', $notice);
        return $this->fetch('notice/noticedetail');
    }
    
    /**
     * 删除消息
     * @return \think\response\Json
     */
    public function noticeDel(){
        $notice_id = input('id');
        $res = Db::table('up_notice')->delete($notice_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}